
function filter(data) {
    let arr = [];
    arr.push(data[0]);
    for (let i = 1; i < data.length; i++) {
        let lastOne =  arr[arr.length -1];
        if (data[i].data === lastOne.data){
            continue;
        } else {
            arr.push(data[i]);
        }
    }
    return arr;
}


function gatherTurning(arr,peakArr,troughArr) {
    for (let i = 1; i <arr.length-1; i++) {
        if (arr[i].data > arr[i-1].data && arr[i].data > arr[i+1].data){
            peakArr.push(arr[i])
        }else if (arr[i].data < arr[i-1].data && arr[i].data < arr[i+1].data){
            troughArr.push(arr[i])
        }
    }
}

function getTurning(data) {
    let peakArr = [];
    let troughArr = [];
    const filteredArr = filter(data);
    gatherTurning(filteredArr,peakArr,troughArr);
    return {
        peakArr:peakArr,
        troughArr:troughArr
    }
}


function smoothFilter(list,window) {
    if (list.length === 0){
        return
    }
    const average = (
        list.reduce((acc,currentVal) => {
            return acc = acc + currentVal.data
        },0) / list.length
    ).toFixed(0)
    list.forEach(item => {
        item.data = average
    })
    return list
}

module.exports =  getTurning
